﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using BOL;
using System.Data.Linq;
using System.Data.SqlClient;
using Website.Logger;
using System.Web.Script.Services;
using System.Web.Services;
using UtilityExtensions;

namespace UILGestion.ObraAdmin.RecepcionDoc
{
    public partial class RecepcionDocListForm : System.Web.UI.Page
    {


        [WebMethod(), ScriptMethod()]
        public static string GetProveedor(String prefixText, String contextKey)
        {

            ProveedorBL bol = new ProveedorBL();
            return bol.getProveedorRecepcionDocAll(prefixText, 20, contextKey.ToInt());

        }

        protected void Page_Load(object sender, EventArgs e)
        {

            proveedortxt.ChkVal = (string)Request.QueryString["idObra"];

            if (!Page.IsPostBack)
            {

                try
                {

                    idObra.Value = (string)Request.QueryString["idObra"];

                    menuObraCtrl1.loadMenu(int.Parse(idObra.Value), Request.AppRelativeCurrentExecutionFilePath);
                    FormatForm();


                }
                catch (SqlException sqlex)
                {
                    WebServerSideSqlError.Log(sqlex);
                    ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                     "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


                }
                catch (Exception ex)
                {
                    WebServerSideError.Log(ex);
                    ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                     "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

                }


            }
        }

        //private void Find()
        //{
        //    dbGestionDataContext _db;

        //    _db = new dbGestionDataContext(Parametros.strConnString);

        //    //(DD/MM/yyyy)
        //    System.IFormatProvider MiFp =
        //                                                new System.Globalization.
        //                                                CultureInfo("es-ES", false);
        //    //valores de parametro

        //    int selectid =  int.Parse(idObra.Value);

        //    //valores de parametro

        //    DateTime fechaInicio = new DateTime(2010, 1, 1);
        //    DateTime fechafin = DateTime.Now;

           

        //    if (inicioDateTime.Text.Length > 0)
        //    {
        //        fechaInicio =
        //            System.DateTime.ParseExact(inicioDateTime.Text, "dd/MM/yyyy", MiFp);
        //    }
        //    if (finDateTime.Text.Length > 0)
        //    {
        //        fechafin =
        //            System.DateTime.ParseExact(finDateTime.Text, "dd/MM/yyyy", MiFp);
        //    }

        //    RecepcionDocBL recBol = new RecepcionDocBL();

        //    List<RecepcionDocListInfo> e = 
        //        recBol.GetRecepcionDocList(int.Parse(idObra.Value));
            
        //    entityListGridView.DataSource = e.ToList();
        //    entityListGridView.DataBind();

            

        //}



        private void FormatForm()
        {

            //restricionesForm();


            ShowAllComboBox();

            inicioDateTime.Text =
                DbUtil.Db.Obras.Single(p => p.idInternal == int.Parse(idObra.Value)).fechaInicio.Value.ToShortDateString();
            finDateTime.Text = DateTime.Now.ToShortDateString();
            proveedortxt.ChkVal = idObra.Value;
         
            RecepcionDocListGrid1.DataBind();

         
        }
        private void ShowAllComboBox()
        {

            ListValueInfo cero= new ListValueInfo();
            cero.descripcion="------Todos----------";
            cero.idInternal=0;
            List<ListValueInfo> lista= new List<ListValueInfo>();
            lista.Add(cero);
            lista.AddRange(new ListValueBL().GetDocumentoTipoRecepcionDoc());
            

            tipoDocumentoDDL.DataTextField = "descripcion";
            tipoDocumentoDDL.DataValueField = "idInternal";
            tipoDocumentoDDL.DataSource = lista;
            tipoDocumentoDDL.DataBind();
        }
        


        protected void nuevoButton_Click(object sender, EventArgs e)
        {

            Response.Redirect("RecepcionDocInsertOPT1Form.aspx?type=A&idObra=" + idObra.Value, true);
        }

        protected void notaCredito_Click(object sender, EventArgs e)
        {

            Response.Redirect("RecepcionDocInsertForm.aspx?type=A&notaCredito=1&idObra=" + idObra.Value, true);
        }

        protected void obraDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                //Find();
                //estadistica();
            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }
        protected void fechaInicioDateTime_TextChanged(object sender, EventArgs e)
        {
            try
            {
                //Find();

            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }


        protected void actualizarButton_Click(object sender, EventArgs e)
        {
            try
            {
                FormatForm();
            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }

        protected void ExportButton_Click(object sender, EventArgs e)
        {
            //enviamos los parametros del listado
              //<asp:querystringparameter  Name="idObra" Type="Int32" querystringfield="idObra"  />
              //  <asp:ControlParameter Name="fechaInicio" Type="DateTime" ControlID="inicioDateTime"  />
              //  <asp:ControlParameter Name="fechaFin" Type="DateTime" ControlID="finDateTime"  />
              //  <asp:Parameter Name="startRowIndex" Type="Int32" />
              //  <asp:Parameter Name="maximumRows" Type="Int32" />
              //  <asp:Parameter Name="sortExpression" Type="String" />

            //ParameterCollection s = recepcionDocListODS.SelectParameters;
            //string idObra = s["idObra"].ToString();
            //string fechaInicio = s["fechaInicio"].ToString();
            //string fechaFin = s["fechaFin"].ToString();
            //string startRowIndex = s["startRowIndex"].ToString();
            //string maximumRows = s["maximumRows"].ToString();
            //string sortExpression = s["sortExpression"].ToString();

            //string todo="idObra={0}&fechaInicio={1}&fechafin={2}&startRowIndex={3}&maximumRows={}";

            Response.Redirect("../../Rpt/ExportExcelForm.aspx?type=CENTROCOSTO&idObra=" + idObra.Value, true);
            
        }

        protected void refreshButton_Click(object sender, EventArgs e)
        {

        }
   
    }
}